<?php


namespace app\admin\service\order;

use app\admin\model\BaseModel;
use app\admin\model\control\GameStock;
use app\admin\model\order\Exchange;
use app\admin\model\platform\GameKindItem;
use app\admin\model\platform\GameRoomInfo;
use app\admin\model\record\RecordModificationStock;
use app\admin\service\BaseService;
use app\admin\service\CommonService;
use think\facade\Cache;
use think\facade\Db;

/**
 * @author tangsan
 * @package app\admin\service
 */
class ExchangeService extends BaseService
{
    /**
     * 构造函数
     * @author tangsan
     * ActionLogService constructor.
     */
    public function __construct()
    {
        $this->model = new Exchange();
    }

    /**
     * 获取数据列表
     * @return array
     * @author tangsan
     */
    public function getList()
    {
        $param = request()->param();
        if (isset($param['startTime']) && $param['startTime']) {
            $startTime = date('Y-m-d 00:00:00', strtotime($param['startTime']));
        } else {
            $startTime = date('Y-m-d 00:00:00', strtotime('-1 day', time()));
        }
        if (isset($param['endTime']) && $param['endTime']) {
            $endTime = date('Y-m-d 00:00:00', strtotime('+1 day', strtotime($param['endTime'])));
        } else {
            $endTime = date('Y-m-d 00:00:00', strtotime('+1 day', time()));
        }

        $sort = 'CollectDate desc';
        $where = [];

        $where[] = ['CollectDate', '>=', $startTime];
        $where[] = ['CollectDate', '<=', $endTime];

        if (isset($param['UserId']) && $param['UserId']){
            $where[] = ['UserId', '=', $param['UserId']];
        }
        if (isset($param['ChannelID']) && $param['ChannelID']){
            $where[] = ['ChannelID', '=', $param['ChannelID']];
        }
        if (isset($param['Status']) && $param['Status']){
            $where[] = ['Status', '=', $param['Status']];
        }
        if (isset($param['OrderID']) && $param['OrderID']){
            $where[] = ['OrderID', '=', $param['OrderID']];
        }

        $result = $this->model->where($where)->order($sort)->page(PAGE, PERPAGE)->select()->toArray();

        if ($result) {
            foreach ($result as &$val) {
                $val['Revenue'] = CommonService::dbToShow($val['Revenue']);
                $val['Score'] = CommonService::dbToShow($val['Score']);
                $val['CollectDate'] = date('Y-m-d H:i:s', strtotime($val['CollectDate']));
                $val['UpdateDate'] = date('Y-m-d H:i:s', strtotime($val['UpdateDate']));
            }
        }
        $count = $this->model->where($where)->count();
        //返回结果
        $message = array(
            "msg" => '操作成功',
            "code" => 0,
            "data" => $result,
            "count" => $count,
        );
        return $message;
    }


}